package com.chinacoal.hr.levelimpl.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinacoal.hr.levelapi.entity.BizTravel;
import com.chinacoal.hr.levelimpl.mapper.BizTravelMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-19 02:01
 * @description: 出差单
 **/
@Component
public class BizTravelDao {

    @Resource
    private BizTravelMapper bizTravelMapper;

    /**
     * 查询商务出差单
     *
     * @param condition :
     * @return : com.baomidou.mybatisplus.core.metadata.IPage<com.chinacoal.hr.levelimpl.entity.BizTravel>
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */
    public IPage<BizTravel> getBizTravelList(Map<String, Object> condition) throws Throwable {
        Long currentPage = (Long) condition.get("page");
        Long currentPageSize = (Long) condition.get("limit");

        IPage<BizTravel> page = new Page(currentPage, currentPageSize);

        String orgId = condition.get("orgId") != null ? condition.get("orgId").toString() : "";
        String bizTravelCode = condition.get("bizTravelCode") != null ? condition.get("bizTravelCode").toString() : "";
        String businessStatus = condition.get("businessStatus") != null ? condition.get("businessStatus").toString() : "";
        String deptCode = condition.get("deptCode") != null ? condition.get("deptCode").toString() : "";
        String deptName = condition.get("deptName") != null ? condition.get("deptName").toString() : "";
        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";
        String queryRange = condition.get("queryRange") != null ? condition.get("queryRange").toString() : "";

        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(orgId)) {
            queryWrapper.eq("OU_ID", orgId);
        }

        if (StringUtils.isNotBlank(bizTravelCode)) {
            queryWrapper.eq("BIZ_TRAVEL_CODE", bizTravelCode);
        }

        if (StringUtils.isNotBlank(businessStatus)) {
            queryWrapper.eq("BUSINESS_STATUS", businessStatus);
        }

        // 查询范围
        if (StringUtils.isNotBlank(queryRange)) {
            if (("all").equals(queryRange)) {
            } else if (("dept").equals(queryRange)) {
                queryWrapper.eq("DEPT_CODE", deptCode);
            } else if (("person").equals(queryRange)) {
                queryWrapper.eq("EMP_CODE", empCode);
            }
        }

        queryWrapper.orderByDesc("LAST_UPDATE_DATE");


        IPage<BizTravel> bizTravelList = bizTravelMapper.selectPage(page, queryWrapper);

        return bizTravelList;
    }

    /**
     * 根据查询条件获得出差申请单信息
     * 如果有多条记录，取第一条
     *
     * @param condition :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/27 2019/11/27
     */
    public BizTravel getBizTravelInfo(Map condition) {
        String travelCode = condition.get("bizTravelCode") != null ? condition.get("bizTravelCode").toString() : "";
        String travelId = condition.get("bizTravelId") != null ? condition.get("bizTravelId").toString() : "";
        String processInstID = condition.get("processInstID") != null ? condition.get("processInstID").toString() : "";

        QueryWrapper wrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(processInstID)) {
            wrapper.eq("INS_CODE", processInstID);
        }

        if (StringUtils.isNotBlank(travelId)) {
            wrapper.eq("BIZ_TRAVEL_ID", travelId);
        }

        if (StringUtils.isNotBlank(travelCode)) {
            wrapper.eq("BIZ_TRAVEL_CODE", travelCode);
        }
        wrapper.last("limit 1");

        BizTravel travelInfo = this.bizTravelMapper.selectOne(wrapper);
        return travelInfo;
    }

    /**
     * 新增商务出差单
     *
     * @param record :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */
    public void insertBizTravel(BizTravel record) {
        bizTravelMapper.insert(record);
    }

    /**
     * 修改商务出差单
     *
     * @param record :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */
    public void updateBizTravel(BizTravel record) {
        bizTravelMapper.updateById(record);
    }

    /**
     * 删除商务出差单
     *
     * @param bizTraveId :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */
    public void deleteBizTravelById(String bizTraveId) {
        bizTravelMapper.deleteById(bizTraveId);
    }
}
